package com.haitaoit.chengquan.module.center.adapter.xuqiu;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyXuqiuDetailModel2;
import com.haitaoit.chengquan.view.CircleImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/12.
 */
public class MyXuqiuDetailAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyXuqiuDetailModel2> list;
    private String type;

    public void setType(String type) {
        this.type = type;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyXuqiuDetailAdapter(Context context) {
        this.context = context;
    }

    public List<MyXuqiuDetailModel2> getList() {
        return list;
    }

    public void setList(List<MyXuqiuDetailModel2> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_xuqiu_detail_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.xuqiu_detail_lookinfo:
                        MyXuqiuDetailAdapter.this.backCall.deal(R.id.xuqiu_detail_lookinfo, position);
                        break;
                    case R.id.xuqiu_detail_choose:
                        MyXuqiuDetailAdapter.this.backCall.deal(R.id.xuqiu_detail_choose, position);
                        break;
                }
            }
        };

        holder.xuqiu_detail_lookinfo.setOnClickListener(listener);
        holder.xuqiu_detail_choose.setOnClickListener(listener);

        // 部分字体变大变色
        SpannableString msp = new SpannableString("报价：¥ " + list.get(position).getOffer_money());
        msp.setSpan(new RelativeSizeSpan(1.5f), 4, ("报价：¥ " + list.get(position).getOffer_money()).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(Color.RED), 3, ("报价：¥ " + list.get(position).getOffer_money()).toString().length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.xiuqiu_baojia_money.setText(msp);

        ImageLoader.getInstance().displayImage(list.get(position).getHead_portrait(), holder.center_img);
        holder.real_name.setText(list.get(position).getReal_name());
        holder.addtime.setText("报价时间：" + list.get(position).getAddtime());

        // order_type 未报价 已报价
        if ("1".equals(type)) {
            holder.xuqiu_detail_choose.setBackgroundColor(context.getResources().getColor(R.color.wheel_timebtn_pre));
        } else if ("2".equals(type)) {
            holder.xuqiu_detail_choose.setBackgroundColor(context.getResources().getColor(R.color.app_color));
        }
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.center_img)
        CircleImageView center_img;
        @ViewInject(R.id.real_name)
        TextView real_name;
        @ViewInject(R.id.addtime)
        TextView addtime;
        @ViewInject(R.id.xiuqiu_baojia_money)
        TextView xiuqiu_baojia_money;
        @ViewInject(R.id.xuqiu_detail_lookinfo)
        TextView xuqiu_detail_lookinfo;
        @ViewInject(R.id.xuqiu_detail_choose)
        TextView xuqiu_detail_choose;
    }
}
